package com.weizhizs.assistant.code.template.data;

import com.weizhizs.assistant.code.config.ParamConfig;
import com.weizhizs.assistant.code.helper.TableHelper;
import com.weizhizs.assistant.code.helper.BeanHelper;
import com.weizhizs.assistant.code.template.IDataLoador;
import com.weizhizs.assistant.code.vo.TableVO;
import com.weizhizs.assistant.utils.DateUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class JavaToData implements IDataLoador {

    public static String getClassName(String tableName) {
        return TableHelper.tableNameToClassName(tableName) + "TO";
    }

    public static String getFullClassName(String classPath, String tableName) {
        return classPath + "." + getClassName(tableName);
    }

    @Override
    public String getTemplateName(TableVO tableVO) {
        return ParamConfig.getInstance().getTemplatePath() + "/to.java.vm";
    }

    @Override
    public String getFileName(String tableName) {
        return getClassName(tableName) + ".java";
    }

    @Override
    public String getFileOutPath() {
        return ParamConfig.getInstance().getToOutPath();
    }

    @Override
    public Map<String, Object> getDataMap(TableVO tableVO) {
        String tableName = tableVO.getTableName();

        Map<String, Object> map = new HashMap<>();
        map.put("tableName", tableName);
        map.put("tableComment", tableVO.readTableComment());
        map.put("tableCommentFull", tableVO.getTableComment());
        map.put("authorName", ParamConfig.getInstance().getAuthorName());
        map.put("version", ParamConfig.getInstance().getVersion());
        map.put("currentDate", DateUtils.nowDateString());

        map.put("pk", tableVO.getPk());
        map.put("allColumns", tableVO.getAllColumns());

        map.put("entityLocalFields", Arrays.asList(ParamConfig.getInstance().getEntityLocalFields()));

        map.put("entityOutPath", getFileOutPath());
        map.put("entityClassName", getClassName(tableName));

        map.put("entityBaseClassName", BeanHelper.getBeanClassName(ParamConfig.getInstance().getToBaseClass()));
        map.put("entityBaseFullClassName", ParamConfig.getInstance().getToBaseClass());

        map.put("verifyFailExceptionClass", ParamConfig.getInstance().getVerifyFailExceptionClass());
        map.put("utilsStringExtUtilsClass", ParamConfig.getInstance().getUtilsStringExtUtilsClass());

        return map;
    }

}
